﻿using System;
using System.Collections.Generic;
using System.Data;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using Gift.DAL;
using Gift.Model;

namespace WebSite.admin.admin
{
    public partial class roleRule : BaseAdmin
    {
        readonly DALRule theRule = new DALRule();
        readonly dalRoleRuleRef theRoleRuleRef = new dalRoleRuleRef();
     

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                base.ValidateLoginRule(Gift.Model.ENUM.ERule.RoleManager);
                this.BuildRuleList();
            }
        }

        private void BuildRuleList()
        {
            StringBuilder strHead = new StringBuilder();
            StringBuilder strBody = new StringBuilder();
            StringBuilder strTable = new StringBuilder();
            StringBuilder strCheckedAdmin = new StringBuilder(",");
            DataSet ds = new DataSet();
            DataSet dsSub = new DataSet();
            string sRoleID = Request.QueryString["rid"];
            ds = theRule.GetListByLevel(0);
            dsSub = theRule.GetListByLevel(1);
            strTable.Append("<table class='tableRule' border=\"1\" cellpadding=\"3\" cellspacing=\"1\" bordercolor=\"#E2E7EB\">");
            if (ds.Tables.Count > 0)
            {
                strHead.Append("<tr class='ruleHead'*>");
                strBody.Append("<tr>");
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    strHead.Append("<td width='100px'>" + dr["Description"].ToString() + "</td>");
                    strBody.Append("<td valign='top'>");
                    if (dsSub.Tables.Count > 0)
                    {
                        dsSub.Tables[0].DefaultView.RowFilter = " parentID = " + dr["ruleID"].ToString();

                        for (int i = 0; i < dsSub.Tables[0].DefaultView.Count; i++)
                        {
                            string sChecked = "";
                            if (theRule.GetRoleRule(int.Parse(dsSub.Tables[0].DefaultView[i]["ruleID"].ToString()), int.Parse(sRoleID)))
                            {
                                sChecked = "checked='checked'";
                                strCheckedAdmin.Append(dsSub.Tables[0].DefaultView[i]["ruleID"] + ",");
                            }
                            strBody.Append("<br/><input type='checkbox' onclick='setAdminId(this.id)' " + sChecked + " id='" + dsSub.Tables[0].DefaultView[i]["ruleID"].ToString() + "'/>" + dsSub.Tables[0].DefaultView[i]["Description"].ToString());
                        }
                    }
                    strBody.Append("</td>");
                }
                strBody.Append("</tr>");
                strHead.Append("</tr>");
            }
            strTable.Append(strHead.ToString());
            strTable.Append(strBody.ToString());
            strTable.Append("</table>");

            this.ltRuleList.Text = strTable.ToString();
            this.hdList.Value = strCheckedAdmin.ToString();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string sRuleIds = string.Empty;
            if (this.hdList.Value.Length > 1)
            {
                sRuleIds = this.hdList.Value.Substring(1, this.hdList.Value.Length - 2);
            }
            else
            {
                sRuleIds = "0,0";
            }

            theRoleRuleRef.Save(int.Parse(Request.QueryString["rid"]), sRuleIds);
            ExtAspNet.Alert.Show("保存成功！", "", "openNewTab('Roleist','/admin/admin/RoleManager.aspx','角色列表','RolePermission')");
            //Response.Write("<script>alert('保存成功！');window.location.href='userlist.aspx';</script>");
        }
    }
}